package com.lanou.lenanapp.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.lanou.lenanapp.R;
import com.lanou.lenanapp.activity.ContributionReviewActivity;
import com.lanou.lenanapp.beans.Contribution;
import com.lanou.lenanapp.view.CircleImage;
import com.lanou.lenanapp.volley.VolleySingleton;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by dllo on 15/10/24.
 */
public class ContributionAdapter extends RecyclerView.Adapter<ContributionAdapter.ContributionViewHolder>{

    private List<Contribution> contributions;//网友投稿页面实体类
    private Context context;
    VolleySingleton volleySingleton = VolleySingleton.getInstance();


    public ContributionAdapter(Context context){
        this.context = context;
    }

    public void addData(List<Contribution> datas){
        contributions = datas;
        notifyDataSetChanged();
    }
    public void loadData(List<Contribution> datas){
        this.contributions.addAll(datas);
        notifyDataSetChanged();

    }


    /** 组件缓存内部类 **/
    class ContributionViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private CircleImage contribution_author_image;
        private TextView contribution_author_name, updata_time_contribution, wbody_contribution,
                review_contribution, like_contribution;
        private ImageView favorited_contribution, transmit_joke_contribution;
        private int position;

        public ContributionViewHolder(View itemView) {
            super(itemView);
            contribution_author_image = (CircleImage) itemView.findViewById(R.id.contribution_author_image);
            contribution_author_name = (TextView) itemView.findViewById(R.id.contribution_author_name);
            updata_time_contribution = (TextView) itemView.findViewById(R.id.updata_time_contribution);
            wbody_contribution = (TextView) itemView.findViewById(R.id.wbody_contribution);
            review_contribution = (TextView) itemView.findViewById(R.id.review_contribution);
            like_contribution = (TextView) itemView.findViewById(R.id.like_contribution);
            favorited_contribution = (ImageView) itemView.findViewById(R.id.favorited_contribution);
            transmit_joke_contribution = (ImageView) itemView.findViewById(R.id.transmit_joke_contribution);
            review_contribution.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.review_contribution:
                    if (contributions != null && contributions.size() > 0){
                        Contribution con = contributions.get(position);
                        Intent intent = new Intent(context, ContributionReviewActivity.class);
                        intent.putExtra("conid", con.getWid());
                        context.startActivity(intent);
                    }
                    break;
            }

        }
    }



    @Override
    public ContributionViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View v = inflater.inflate(R.layout.activity_contribution_gut, null);

        return new ContributionViewHolder(v);
    }


    @Override
    public void onBindViewHolder(ContributionViewHolder holder, int position) {

        if (contributions != null && contributions.size() > 0){

            Contribution con = contributions.get(position);
            holder.position = position;

            holder.contribution_author_name.setText(con.getUser_name());

            double t = Double.parseDouble(con.getLikes());
            int li = (int) t;
            holder.like_contribution.setText(li + "");

            holder.wbody_contribution.setText(con.getWbody());
            holder.review_contribution.setText(con.getComments());

            String time = getNormalTime(con.getUpdate_time());
            holder.updata_time_contribution.setText(time);

            String url = con.getUser_avatar();
            getBitMap(url, holder.contribution_author_image);

        }

    }

    private void getBitMap(String url, final ImageView imageView) {
      final ImageRequest request = new ImageRequest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                imageView.setImageBitmap(response);
            }
        }, 1000, 1000, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        volleySingleton.addRequest(request);

    }


    /**
     * 时间格式转换方法
     * **/
    public  String  getNormalTime( String value) {
        Long l = Long.valueOf(value);
        l = l * 1000;
        Date date = new Date(l);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ;
        String time = format.format(date) ;
        return time;
    }


    @Override
    public int getItemCount() {

        return contributions != null && contributions.size() > 0 ? contributions.size() : 0;
    }






}
